<?php

namespace App\Http\Controllers;

use App\Repositories\TopicRepository;
use Illuminate\Http\Request;
use App\Models\ChinaArea;

class CommonController extends Controller
{
    public function province()
    {
        $list = ChinaArea::query()->where("parent_id", 1)->get(['id', 'name']);
        return normalize(0, "OK", $list->toArray());
    }
    
    public function city()
    {
        $pid = request('pid');
        if (empty($pid))
        {
            return normalize("no pid");
        }
        $list = ChinaArea::query()->where('parent_id', $pid)->get(['id', 'name']);
        return normalize(0, "OK", $list->toArray());
    }
    
    public function district()
    {
        $pid = request('pid');
        if (empty($pid))
        {
            return normalize("no pid");
        }
        $list = ChinaArea::query()->where('parent_id', $pid)->get(['id', 'name']);
        return normalize(0, "OK", $list->toArray());
    }

    public function provinceCityDistrict(Request $request)
    {
        $result = (new TopicRepository())->provinceCityDistrict($request);

        return api(RET_OK, MSG_OK, $result);
    }

    public function listAreaByPid($pid)
    {
        if (empty($pid))
        {
            return normalize("no pid");
        }
        $list = ChinaArea::query()->where('parent_id', $pid)->get(['id', 'name']);
        return normalize(0, "OK", $list->toArray());
    }

}
